package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description DishServiceImpl
 * @Author zhuzi
 * @Date 2023/09/25
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void createDish(DishDTO dishDTO) {
        // 判断菜品是否存在
        Dish dish = dishMapper.getDishByName(dishDTO.getName());
        if (dish != null) {
            throw new BaseException("菜品已存在");
        }

        // 新增菜品
        dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);

        // 新增口味
        Long dishId = dish.getId();
        log.info("Get DishId: {}", dishId);
        List<DishFlavor> dishFlavors = dishDTO.getFlavors().stream().map((i) -> {
            DishFlavor dishFlavor = new DishFlavor();
            BeanUtils.copyProperties(i, dishFlavor);
            dishFlavor.setDishId(dishId);
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorsMapper.saveDishFlavors(dishFlavors);
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page page = (Page) dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    /**
     * 启用或禁用菜品
     *
     * @param status
     * @param id
     */
    @Override
    public void pickOrBan(Integer status, Long id) {
        // 校验数据合法性：菜品是否存在、是否是启用状态启用或禁止状态禁止
        Dish dish = dishMapper.getDishByDishId(id);
        log.info("Get dish: {}", dish);
        if (dish == null || dish.getId() == null || dish.getStatus().equals(status)) {
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }

        // 判断菜品是否关联套餐
        if (setmealDishMapper.getCountByDishId() > 0) {
            throw new BaseException("当前菜品关联了套餐，不能停售");
        }

        // 修改
        dish.setStatus(status);
        dishMapper.updateStatusById(dish);
    }

    /**
     * 根据id获取菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        Dish dish = dishMapper.getDishByDishId(id);
        List<DishFlavor> dishFlavors = dishFlavorsMapper.getDishFlavorsByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        // 修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateByDishId(dish);

        // 删除菜品口味
        dishFlavorsMapper.deleteDishFlavorsByDishId(dish.getId());

        // 添加菜品口味
        List<DishFlavor> dishFlavors = dishDTO.getFlavors().stream().map((i) -> {
            i.setDishId(dish.getId());
            return i;
        }).collect(Collectors.toList());
        dishFlavorsMapper.saveDishFlavors(dishFlavors);
    }

    /**
     * 根据id删除菜品
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        // 判断菜品是否起售
        if (dishMapper.getCountOfEnableByids(ids) > 0) {
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }

        // 删除菜品
        dishMapper.deleteByIds(ids);

        // 删除口味
        dishFlavorsMapper.deleteDishFlavorsByDishIds(ids);
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listDish(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            // 根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getDishFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
